//
//  PrizeViewController.m
//  GiftSpeak
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PrizeViewController.h"
#import "DefineURL.h"
//#import "GSAFNetWorkTool.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"
#import "DetailViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "AsynURLConnection.h"

@interface PrizeViewController ()<UITableViewDataSource,UITableViewDelegate,MovieTableViewCellDelegate>
@property(nonatomic,retain)UITableView *table;
@property(nonatomic,retain)NSMutableArray *tabArr;
@property(nonatomic,strong)AVPlayer *player;//播放器对象
@property (retain, nonatomic)UIView *container; //播放器容器
@property(nonatomic,retain)UIButton *playOrPause;//播放/暂停按钮
@property(nonatomic,retain)UIProgressView *progress;

@end

@implementation PrizeViewController
-(void)dealloc
{
    [_tabArr release];
    [_table release];
    
    [super dealloc];
}


/**
 *  搞笑视频的详情界面
 */
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTbaleView];
    [self praser];
    [self createLeftButton];
    self.view.backgroundColor = [UIColor whiteColor];
}
-(void)createLeftButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30*OffWidth, 30*OffHeight)];
    
    [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    [button release];

}
-(void)click:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)praser
{
    //http://api.budejie.com/api/api_open.php?a=list&appname=baisishequ&asid=1DDDF06B-87A4-4AE7-B7CF-8819C61B0EB1&c=video&client=iphone&device=iPhone%205&from=ios&jbk=1&mac=&market=&openudid=72244dd2b62c5bc75b9c5ff90be56501274eaafb&page=0&per=20&sub_flag=1&type=41&udid=&ver=3.6.1
   //http://api.budejie.com/api/api_open.php?a=dataList&appname=baisishequ&asid=1DDDF06B-87A4-4AE7-B7CF-8819C61B0EB1&c=comment&client=iphone&data_id=15810774&device=iPhone%205&hot=1&jbk=1&mac=&market=&openudid=72244dd2b62c5bc75b9c5ff90be56501274eaafb&page=1&per=50&udid=&ver=3.6.1
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"list" forKey:@"a"];
    [dic setValue:@"baisishequ" forKey:@"appname"];
    [dic setValue:@"1DDDF06B-87A4-4AE7-B7CF-8819C61B0EB1" forKey:@"asid"];
    [dic setValue:@"video" forKey:@"c"];
    [dic setValue:@"iphone" forKey:@"client"];
    [dic setValue:@"iPhone%205" forKey:@"device"];
    [dic setValue:@"ios" forKey:@"from"];
    [dic setValue:@"1" forKey:@"jbk"];
    [dic setValue:@"" forKey:@"mac"];
    [dic setValue:@"" forKey:@"market"];
    [dic setValue:@"72244dd2b62c5bc75b9c5ff90be56501274eaafb" forKey:@"openudid"];
    [dic setValue:@"0" forKey:@"page"];
    [dic setValue:@"20" forKey:@"per"];
    [dic setValue:@"1" forKey:@"sub_flag"];
    [dic setValue:@"41" forKey:@"type"];
    [dic setValue:@"" forKey:@"udid"];
    [dic setValue:@"3.6.1" forKey:@"ver"];

    [AsynURLConnection asynWithURL:@"http://api.budejie.com/api/api_open.php" parmaters:dic block:^(NSData *data) {
        
        NSDictionary *root = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"root = %@",root);
        NSArray *arr = [root objectForKey:@"list"];
        for (int i = 0; i < arr.count; i++) {
            MovieModel *model = [[MovieModel alloc]init];
            [model setValuesForKeysWithDictionary:arr[i]];
            [self.tabArr addObject:model];
        }
        [self.table reloadData];
    }];
       [self.table reloadData];
}
-(void)createTbaleView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.table.rowHeight = 300*OffHeight;
    [self.view addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tabArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"movie";
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[MovieTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]autorelease];
    }
    MovieModel *model = [self.tabArr objectAtIndex:indexPath.row];
    [cell sendModel:model];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    return cell;
}
#pragma mark - 点击播放按钮 播放视频
-(void)clickImage:(MovieModel *)model
{
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.url = model.videouri;
    detail.ids = model.id;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
